﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace Client
{
    internal class Program
    {
        static void Main(string[] args)
        {
            byte[]data = new byte[1024];
            Socket ClientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
            Console.WriteLine("Please write Server IPAdress:");
            string IPAdress = Console.ReadLine();
            Console.WriteLine();
            Console.Write("Please Write Server Port:");
            int nPort = Convert.ToInt32(Console.ReadLine());
            IPEndPoint IP = new IPEndPoint(IPAddress.Parse(IPAdress),nPort);
            try
            {
                ClientSocket.Connect(IP);
            }
            catch (SocketException e)
            {
                Console.WriteLine("Can Not Connet Server!");
                Console.WriteLine(e.ToString());
                return;
            }
            int nRetByte = ClientSocket.Receive(data);
            string strData = Encoding.ASCII.GetString(data,0, nRetByte);
            Console.WriteLine(strData);
            while (true)
            {
                string strInput = Console.ReadLine();
                if (strInput == "exit")
                    break;
                String strSay = " Client Say:"; 
                ClientSocket.Send(Encoding.ASCII.GetBytes(strSay + strInput));
                data = new byte[1024];
                nRetByte = ClientSocket.Receive(data);
                strData = Encoding.ASCII.GetString(data, 0, nRetByte);
                Console.WriteLine(strData);
            }
            Console.WriteLine("DisConnect From Server");
            ClientSocket.Shutdown(SocketShutdown.Both);
            ClientSocket.Close();
        }
    }
}
